/**
 * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
 * @file      main.c
 * @author    Fortiortech  Appliction Team
 * @since     Create:2021-08-13
 * @date      Last modify:2022-08-13
 * @note      Last modify author is Leo.li
 * @brief     包含Lin初始化，数据处理
 */

#include <Myproject.h>

uint8 xdata ucaLinBuff[8] = {0x68, 0x04, 0x12, 0x35, 0x79, 0xab, 0xcd, 0xef};

/**
 * @brief     Lin初始化
 * @date      2022-08-13
 */
void LIN_Init(void)
{
/*-------------------------------------------------------------------------------------------------
    DMA通道配置
    DMASEL=1:配置为DMA1 DMASEL=0:配置为DMA0
-------------------------------------------------------------------------------------------------*/
#if LIN_DMA_Pipe == 1
    DMA1_BA = ucaLinBuff;
    SetBit(DMA1_CR0, DMAEN | DMACFG2 | DMACFG1);
#elif LIN_DMA_Pipe == 0
    DMA0_BA = ucaLinBuff;
    SetBit(DMA0_CR0, DMAEN | DMACFG2 | DMACFG1);
#endif
    ClrBit(LIN_CR, DMASEL);
    /*-------------------------------------------------------------------------------------------------
    LIN通道配置 CHSEL
    00:配置为TXD：P00 RXD：p01;
    10:配置为TXD：P10 RXD：p11;
    11:单线lin P11
    注意：6806的P10P11没有内部上拉，端口转移时，P10需配置外部上拉
    ------------------------------------------------------------------------------------------------*/
    SetBit(LIN_CR, CHSEL1); // 通道配置
    SetBit(LIN_CR, CHSEL0); // 通道配置
    SetBit(P1_PU, P11HV_EN);

    //    ClrBit(LIN_CR, CHSEL1);                            // 通道配置
    //    ClrBit(LIN_CR, CHSEL0);                            // 通道配置

    ClrBit(LIN_CR, CHKMOD);   // 0:增强校验    1:标准校验
    ClrBit(LIN_CR, AUTOSIZE); // 自动长度设定（ID[4][5]决定）
    SetBit(LIN_CR, LINIE);    // 中断设置
    SetBit(LIN_CSR, LINEN);   // LIN模块使能
}

/* -------------------------------------------------------------------------------------------------
    Function Name  : Sync_Error_Check
    Description    : 检验是否发生同步错误
    Date           : 2020-07-30
    Parameter      : None
------------------------------------------------------------------------------------------------- */
ebool Sync_Error_Check(void)
{
    return ReadBit(LIN_SR, ERRSYNC);
}

/* -------------------------------------------------------------------------------------------------
    Function Name  : Data_Error_Check
    Description    : 检验是否发生数据检验错误
    Date           : 2020-07-30
    Parameter      : None
------------------------------------------------------------------------------------------------- */
ebool Data_Error_Check(void)
{
    return ReadBit(LIN_SR, ERRCHK);
}

/**
 * @brief        检验是否发生ID校验错误
 * @return       错误源信号
 * @date         2022-08-14
 */
ebool ID_Error_Check(void)
{
    return ReadBit(LIN_SR, ERRPRTY);
}

/* -------------------------------------------------------------------------------------------------
    Function Name  : LIN_Error_Handing
    Description    : 处理错误的函数
    Date           : 2020-07-30
    Parameter      : None
------------------------------------------------------------------------------------------------- */
/**
 * @brief        Lin通信错误源
 * @date         2022-08-14
 */
void LIN_Error_Handing(void)
{
    if (Sync_Error_Check()) // 同步校验错误执行语句
    {
        ;
    }
    else if (Data_Error_Check()) // 数据校验错误执行语句
    {
        ;
    }
    else if (ID_Error_Check()) // ID校验错误执行语句
    {
        ;
    }
}

void LIN_Schdule(void)
{
    LIN_SETADDR(ucaLinBuff);

    switch (LIN_ID)
    {
    case 0x00:
        LIN_SETSIZE(8);
        LINRW_R;
        break;

    case 0x01:
        LIN_SETSIZE(8);
        LINRW_W;
        break;

    case 0x02:
        LIN_SETSIZE(8);
        LINRW_R;
        break;

    case 0x1B:
        LIN_SETSIZE(8);
        LINRW_W;
        break;

    case 0x28:
        LIN_SETSIZE(8);
        LINRW_W;
        break;

    case 0x3A:
        LIN_SETSIZE(8);
        LINRW_R;
        break;

    case 0x3B:
        LIN_SETSIZE(8);
        LINRW_W;
        break;

    case 0x3C:
        LIN_SETSIZE(8);
        LINRW_R;
        break;

    case 0x3D:
        ucaLinBuff[0] = 0x68;
        ucaLinBuff[1] = 0x04;
        ucaLinBuff[2] = 0x12;
        ucaLinBuff[3] = 0x35;
        ucaLinBuff[4] = 0x79;
        ucaLinBuff[5] = 0xab;
        ucaLinBuff[6] = 0xcd;
        ucaLinBuff[7] = 0xef;
        LIN_SETSIZE(8);
        LINRW_W;
        break;

    default:
        break;
    }

    LIN_SETACK();
}